﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ZipCorpus.cs" company="Arizona State University">
//   Copyright (C) 2012 Sukru Tikves and Arizona State University
//   
//   This is unreleased reasearch code, not licensed for external use, nor distribution.
//   Any use outside ASU CIPS Group is prohibited. 
//   
//   Public releases will have a different license.
// </copyright>
// <summary>
//   Defines the ZipCorpus type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Cips.DataMining.IO
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    using Ionic.Zip;

    /// <summary>
    /// The zip corpus.
    /// </summary>
    public static class ZipCorpus
    {
        public static Dictionary<string, string> Read(Stream stream)
        {
            using (var zip = ZipFile.Read(stream))
                return zip.Where(e => !e.IsDirectory)
                    .ToDictionary(e => e.FileName, e => new StreamReader(e.OpenReader()).ReadToEnd());
        }

        public static Dictionary<string, T> Read<T>(Stream stream, Func<string, T> textFunction)
        {
            using (var zip = ZipFile.Read(stream))
                return zip.Where(e => !e.IsDirectory)
                    .ToDictionary(e => e.FileName, e => textFunction(new StreamReader(e.OpenReader()).ReadToEnd()));
        }

        public static Dictionary<string, T> Read<T>(Stream stream, Func<string, T> textFunction, Func<string, bool> filter)
        {
            using (var zip = ZipFile.Read(stream))
                return zip.Where(e => !e.IsDirectory && filter(e.FileName))
                    .ToDictionary(e => e.FileName, e => textFunction(new StreamReader(e.OpenReader()).ReadToEnd()));
        }

        public static Dictionary<string, T> Read<T>(Stream stream, Func<string, string, T> textFunction)
        {
            using (var zip = ZipFile.Read(stream))
                return zip.Where(e => !e.IsDirectory)
                    .ToDictionary(e => e.FileName, e => textFunction(e.FileName, new StreamReader(e.OpenReader()).ReadToEnd()));
        }

        public static Dictionary<string, T> Read<T>(Stream stream, Func<string, string, T> textFunction, Func<string, bool> filter)
        {
            using (var zip = ZipFile.Read(stream))
                return zip.Where(e => !e.IsDirectory && filter(e.FileName))
                    .ToDictionary(e => e.FileName, e => textFunction(e.FileName, new StreamReader(e.OpenReader()).ReadToEnd()));
        }

        public static void Process(Stream stream, Action<string, string> textProcessor)
        {
            using (var zip = ZipFile.Read(stream))
                foreach (var item in zip.Where(e => !e.IsDirectory))
                    textProcessor(item.FileName, new StreamReader(item.OpenReader()).ReadToEnd());
        }

        public static void Process(Stream stream, Action<string, string> textProcessor, Func<string, bool> filter)
        {
            using (var zip = ZipFile.Read(stream))
                foreach (var item in zip.Where(e => !e.IsDirectory && filter(e.FileName)))
                    textProcessor(item.FileName, new StreamReader(item.OpenReader()).ReadToEnd());
        }
    }
}
